package com.alfred.springboot.handlerinterceptor.config;

import com.alfred.springboot.handlerinterceptor.interceptor.LoginPermissionInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author: alfred
 * @date: 2021/7/20
 * @description: WebMvc配置类
 */
@Configuration
public class ServletContextConfig implements WebMvcConfigurer {

	@Resource
	LoginPermissionInterceptor loginPermissionInterceptor;

	/**
	 * 开启跨域
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		// 设置允许跨域的路由
		registry.addMapping("/**")
				// 设置允许跨域请求的域名
				.allowedOrigins("*")
				// 是否允许证书（cookies）
				.allowCredentials(true)
				// 设置允许的方法
				.allowedMethods("*")
				// 设置允许的表头
				.allowedHeaders("*")
				// 跨域允许时间
				.maxAge(3600);
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(loginPermissionInterceptor).addPathPatterns("/*");
	}

}
